package com.catbad.services.impl;

import com.catbad.domain.Filmorder;
import com.catbad.mapper.FilmorderMapper;
import com.catbad.services.OrderServices;
import com.catbad.vo.OrderVO;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class OrderServicesImpl implements OrderServices {
    @Autowired
    FilmorderMapper filmorderMapper;
    @Override
    public List<String> getSeatsByPid(Long pid) {
        List<String> seats = new ArrayList<>();
        List<Filmorder> orderList = filmorderMapper.getSeatsByPid(pid);
        for(Filmorder order:orderList){
            String[] seatList = order.getSeat().split(",");
            for (String s : seatList) {
                seats.add(s);
            }
        }
        return seats;
    }

    @Override
    public Integer saveOrder(Filmorder filmorder) {
        //已有数据，pid，cntprice,uid(由session获得）
        //设置orderId,statu,creatTime
        //设置orderId
        filmorder.setOrderId(getOrderId(filmorder));
        //creatTime
        Date date = new Date(System.currentTimeMillis());
        filmorder.setCreateTime(date);
        //statu
        filmorder.setStatu("1");
        //将seat转换正确
        filmorder.setSeat(formatSeat(filmorder.getSeat()));
        return filmorderMapper.saveOrder(filmorder);
    }


    @Override
    public OrderVO getOrderPage(Integer pageNo,Integer rows,Long uid) {
        //分页首次进入为null
        if(pageNo==null){
            pageNo=1;
        }
        if(pageNo<1){
            pageNo=1;
        }
        if(rows==null){
            rows=4;
        }
        Long total = filmorderMapper.total(uid);
        Integer maxPage = (int)(total%rows==0?total/rows:total/rows+1);
        maxPage = Math.max(1,maxPage);
        if(pageNo>maxPage){
            pageNo = maxPage;
        }
        int start = (pageNo-1)*rows;
        int length = rows;
        Map<String,Object> params = new HashMap<>();
        //添加查询条件
        params.put("start",start);
        params.put("length",length);
        params.put("uid",uid);
        List<Map<String,Object>> orderList= filmorderMapper.getOrderMsgByUid(params);
        OrderVO orderVO = new OrderVO();

        orderVO.setOrderList(orderList);
        orderVO.setPageNo(pageNo);
        orderVO.setTotal(total);
        orderVO.setMaxPage(maxPage);
        orderVO.setRows(rows);

        return orderVO;
    }

    //获得不重复订单号,线程安全
    public synchronized  String getOrderId(Filmorder filmorder){
        String first =String.valueOf(System.currentTimeMillis());
        Random r = new Random();
        int last = r.nextInt(900000)+100000;//(int)(Math.random()*999999)
        return first+last;
    }
    //将1-22-33-4这种形式的转化为1-2，2-3，3-4
    public String formatSeat(String seatStr){
        char arr[]= seatStr.toCharArray();
        String seat = "";
        for(int i=0;i<arr.length;){
            seat = seat + ","+arr[i] + arr[i + 1] + arr[i + 2];
            i+=3;
        }
        seat = seat.substring(1,seat.length());
        return seat;
    }

}
